//
//  UserAccout.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/11.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit

class UserAccout: NSObject,NSCoding {
    
    //获取用户信息...
    var access_token: String?
    var expires_in: NSTimeInterval = 0
    var uid: String?
    var avatar_large: String?
    var name: String?
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String {
        let keys = ["access_token", "expires_in", "uid", "avatar_large", "name"]
        return dictionaryWithValuesForKeys(keys).description
    
    }
    //解归档
    class func loadAccout() -> UserAccout? {
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccout {
            return account
        }
        return nil
    }
    
    //MARK: 实现NSCoding 协议方法
    
    //解归档  将 磁盘中的二进制数据 -> 转换成自定义对象  跟反序列化很像
   
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        }
    
    //归档 将对象 转换二进制  保存到磁盘中  序列化
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
}
